package com.yixing.tech.wms.controller;

import com.yixing.tech.common.base.BaseControl;
import com.yixing.tech.common.base.BaseResponse;
import com.yixing.tech.common.base.Rs;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.wms.param.InventoryOrderOptParam;
import com.yixing.tech.wms.param.InventoryOrderSaveParam;
import com.yixing.tech.wms.service.InventoryService;
import com.yixing.tech.wms.stock.out.callback.error.TaskErrorCallbackVO;
import com.yixing.tech.common.entity.WCSTaskCallbackVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 盘点
 *
 * @author yixing tech
 * @since 1.0.0
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping(value = "/inventory/order")
public class InventoryOrderController extends BaseControl {

    @Autowired
    private InventoryService inventoryService;

    @PostMapping("/save")
    public Rs<Object> save(@RequestBody InventoryOrderSaveParam param) throws BusinessException {
        return Rs.ok(inventoryService.save(param));
    }

    /**
     * 下架：根据盘点单
     */
    @PostMapping("/out")
    public Rs<Object> out(@RequestBody InventoryOrderOptParam param) throws BusinessException {
        return Rs.ok(inventoryService.out(param.getOrderCode()));
    }

//    /**
//     * 返仓：根据盘点作业
//     */
//    @PostMapping("/finishBack")
//    public Rs<Object> finishBack(@RequestBody InventoryOrderOptParam param) throws BusinessException {
//        return Rs.ok(inventoryService.finishBack(param.getWorkCode()));
//    }

    /**
     * 扫描容器展示盘点作业信息
     */
    @PostMapping("/detail")
    public Rs<Object> detail(@RequestBody InventoryOrderOptParam param) throws BusinessException {
        return Rs.ok(inventoryService.detail(param.getContainerCode()));
    }

    /**
     * 盘点确认
     */
    @PostMapping("/check")
    public Rs<Object> check(@RequestBody InventoryOrderOptParam param) throws BusinessException {
        return Rs.ok(inventoryService.check(param.getWorkCode(), param.getRealNum(), param.getInventoryNote()));
    }

    /**
     * 上报Erp
     */
    @PostMapping("/report")
    public Rs<Object> report(@RequestBody InventoryOrderOptParam param) throws BusinessException {
        return Rs.ok(inventoryService.reportWork(param.getWorkCode()));
    }

    @PostMapping("/callback")
    public Rs<Object> productCallEmptyCallback(@RequestBody WCSTaskCallbackVO callbackVO) throws BusinessException {
        log.info("盘点 callback, 请求参数:{}", callbackVO);

        inventoryService.callback(callbackVO.getTaskCode(), callbackVO.getMethod(),
                callbackVO.getCurrentPositionCode(), callbackVO.getPodCode());

        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(1000);
        response.setDesc("任务节点反馈接收成功!");
        return response;
    }

    @PostMapping("/error/callback")
    public Rs<Object> productCallEmptyErrorCallback(@RequestBody TaskErrorCallbackVO callbackVO) throws BusinessException {
        log.error("盘点 errorCallback, 请求参数:{}", callbackVO);

        Object data = inventoryService.errorCallback(callbackVO);
        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(1000);
        response.setData(data);
        response.setDesc("任务节点反馈接收成功!");
        return response;
    }

}
